package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"novel-mgmt-master/cache"
	"novel-mgmt-master/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_no := c.PostForm("car_no")
	car_name := c.PostForm("car_name")
	car_code := c.PostForm("car_code")

	data := models.Car{
		CarNo:   car_no,
		CarName: car_name,
		CarCode: car_code,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_from := c.PostForm("order_from")
	order_to := c.PostForm("order_to")
	order_good := c.PostForm("order_good")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.CarOrder{
		OrderNo:   order_no,
		OrderFrom: order_from,
		OrderTo:   order_to,
		OrderGood: order_good,
		Cid:       cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	/*SELECT
	   car_orders.id as OrderId,
	   car_orders.order_no as OrderNo,
	   car_orders.order_from as OrderFrom,
	   car_orders.order_to as OrderTo,
	   car_orders.order_good as OrderGood,
	   cars.car_no as CarNo,
	   cars.car_name as CarName,
	   Cars.car_code as CarCode
	  FROM
	  car_orders
	  LEFT JOIN cars ON cars.id=car_orders.cid
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var data []models.CarOrderList
	redis_key := "car_order_list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"Cars.car_code as CarCode",
			).
			Joins("LEFT JOIN cars ON cars.id=car_orders.cid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&data)

		jsonData, _ := json.Marshal(data)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	var total int64
	models.DB.Model(&models.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  data,
		"total": total,
	})
	return
}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var data models.CarOrderList
	redis_key := "car_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"Cars.car_code as CarCode",
			).
			Joins("LEFT JOIN cars ON cars.id=car_orders.cid").
			Where("car_orders.id=?", id).
			Debug().
			Find(&data)

		jsonData, _ := json.Marshal(data)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
